import { _decorator, color, Component, find, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Equipment } from './Equipment';
import { Core } from '../Core/Core';
import { EEquipmentType, ItemQualityColors } from './Item';
import { Context } from '../Context';
import { EEvent } from '../Config/Enum';
import { ResourceManager } from '../Core/ResourceManager';
import { PicPath } from '../Config/ResourceConfig';
const { ccclass, property } = _decorator;


//装备格子
@ccclass('EquipmentItem')
export class EquipmentItem extends Component {

    ndEquipmentDetail: Node | null = null;

    spBg: Sprite | null = null;
    ndGem: Node | null = null;





    equipment: Equipment

    protected onLoad(): void {
        

    }

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_END, this.onButtonShowDetail, this);
        this.equipment = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(this.node.name);
        //Core.Instance.event.on(EEvent.EquipmentChanged, this.onUpdateEquipmentItem, this);
        this.ndEquipmentDetail = this.node.getParent().getParent().getChildByName("EquipmentDetail");
        this.ndGem = this.node.getChildByName("Gem");
        //this.showIcon();
        this.loadGem();

        
        
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_END, this.onButtonShowDetail, this);
        //Core.Instance.event.off(EEvent.EquipmentChanged, this.onUpdateEquipmentItem, this);
        
    }

    start() {

    }

    update(deltaTime: number) {
        
    }

    showIcon() {
        // this.equipment = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(this.node.name);
        // if (!this.equipment) {
        //     console.log("当前装备不存在:", this.node.name);
        //     this.node.getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(PicPath.defaultEquipmentIcon); // 设置默认图标
        //     return;
        // }


        // // 加载图标
        // console.log("showIcon in EquipmentItem, this.equipment:", this.equipment);
        // console.log("showIcon in EquipmentItem, this.equipment.icon:", this.equipment.icon);
        // this.node.getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(this.equipment.icon);
        // this.node.getChildByName("BG").getComponent(Sprite).color = color().fromHEX(ItemQualityColors[this.equipment.quality]); // 设置背景颜色为白色
    }

    loadGem() {
        if (!this.equipment) return;
        
        // if (this.isEquipment(this.itemData)) {
        //     //console.log("11111BagItem loadGem this.itemData 是 IEquipment:", this.itemData);
        //     // 现在 TypeScript 知道 this.itemData 是 IEquipment
        //     const equipment = this.itemData;
        //     //console.log("11111BagItem装备的宝石插槽数:", equipment.gemSlots);
            
        //     // 可以安全访问 equipment.gemSlots
            if (this.ndGem) {
                this.ndGem.active = this.equipment.gemSlots > 0;
                switch (this.equipment.gemSlots) {
                    case 0:
                        this.ndGem.active = false;
                        console.log("11111BagItem装备没有宝石插槽");
                        break;
                    case 1:
                        this.ndGem.getChildByName("Gem1").active = true;
                        this.ndGem.getChildByName("Gem2").active = false;
                        this.ndGem.getChildByName("Gem3").active = false;
                        console.log("11111BagItem装备有1个宝石插槽");
                        break;
                    case 2:
                        this.ndGem.getChildByName("Gem1").active = true;
                        this.ndGem.getChildByName("Gem2").active = true;
                        this.ndGem.getChildByName("Gem3").active = false;
                        console.log("11111BagItem装备有2个宝石插槽");
                        break;
                    case 3:
                        this.ndGem.getChildByName("Gem1").active = true;
                        this.ndGem.getChildByName("Gem2").active = true;
                        this.ndGem.getChildByName("Gem3").active = true;
                        break;
                    default:
                        console.warn(`装备 ${this.equipment.name} 的宝石插槽数异常: ${this.equipment.gemSlots}`);
                        this.ndGem.active = false;
                        break;
                }
                
                // const gemLabel = this.ndGem.getComponent(Label);
                // if (gemLabel && equipment.gemSlots > 0) {
                //     gemLabel.string = `${equipment.gemSlots}`;
                // }
            }
        
    }



    onButtonShowDetail() {
        if(this.node.parent.parent.name === "EquipmentDetail"){return}
        
        Core.Instance.currentEquipmentType = this.node.name as EEquipmentType;
        console.log("点击了物品", this.node.name);
        console.log("Core.Instance.currentEquipmentType:", Core.Instance.currentEquipmentType);
        // 如果没有装备则return
        if (!Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.has(Core.Instance.currentEquipmentType)) {
            //console.error("当前玩家没有装备:", Core.Instance.currentEquipmentType);
            Context.ndEquipmentSwitchView.active = true;
            return;
            
        }



        // 显示EquipmentDetail节点
        this.ndEquipmentDetail.active = true;


    }

    onUpdateEquipmentItem(){
        //this.equipment = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(Core.Instance.currentEquipmentType);
        console.log("装备更新事件收到 in equipmentItem, this.equipment:", this.equipment);
        //this.showIcon();
    }



}


